一天一个Linux命令(第6天touch命令)

touch命令

  • touch命令有两个功能:一是用于把已存在文件的时间标签更新为系统当前的时间(默认方式),它们的数据将原封不动地保留下来;二是用来创建新的空文件。

    当然了touch主要的功能是用来创建空文件,至少我常用来这么做。

语法

touch(选项)(参数)

选项:

-a:或--time=atime或--time=access或--time=use  只更改存取时间;
-c:或--no-create  不建立任何文件;
-d:<时间日期> 使用指定的日期时间,而非现在的时间;
-f:此参数将忽略不予处理,仅负责解决BSD版本touch指令的兼容性问题;
-m:或--time=mtime或--time=modify  只更该变动时间;
-r:<参考文件或目录>  把指定文件或目录的日期时间,统统设成和参考文件或目录的日期时间相同;
-t:<日期时间>  使用指定的日期时间,而非现在的时间;
--help:在线帮助;
--version:显示版本信息。

实例

创建文件夹dongpengju,可以看到文件夹大小为0,是为空的:

[fenglangjuxu@localhost usr]$ touch dongpengju
touch: 无法创建"dongpengju": 权限不够
[fenglangjuxu@localhost usr]$ su root
密码:
[root@localhost usr]# pwd
/usr
[root@localhost usr]# touch dongpengju
[root@localhost usr]# ls
bin         etc    include  lib64    local  share  tmp
dongpengju  games  lib      libexec  sbin   src
[root@localhost usr]# ll
总用量 264
dr-xr-xr-x.   2 root root 49152 6月  22 04:37 bin
-rw-r--r--.   1 root root     0 6月  25 06:55 dongpengju
drwxr-xr-x.   2 root root     6 4月  11 2018 etc
drwxr-xr-x.   2 root root     6 4月  11 2018 games
drwxr-xr-x.   9 root root  4096 6月  22 04:34 include
dr-xr-xr-x.  42 root root  4096 6月  22 04:36 lib
dr-xr-xr-x. 142 root root 81920 6月  22 04:39 lib64
drwxr-xr-x.  49 root root 12288 6月  22 04:36 libexec
drwxr-xr-x.  12 root root   131 6月  22 04:07 local
dr-xr-xr-x.   2 root root 20480 6月  22 04:37 sbin
drwxr-xr-x. 235 root root  8192 6月  22 04:37 share
drwxr-xr-x.   4 root root    34 6月  22 04:07 src
lrwxrwxrwx.   1 root root    10 6月  22 04:07 tmp -> ../var/tmp
[root@localhost usr]#